Option Infer On
Imports System.Data.SqlClient
Imports System.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Xml.Linq
Imports System.Linq
Imports System.Text
Imports System.Data.SqlTypes

Namespace BusinessLayer
    Public Class cAddModifyFund

#Region "Declarations"
        Private m_FundId As Integer = 0
        Private m_FndEntityID As String
        Private m_FundName As String = ""
        Private m_StatusID As Integer = -1
        Private m_FundTypeId As Integer = -1
        Private m_styleId As Integer = -1
        Private m_TurnoverId As Integer = -1
        Private m_CountryId As Integer = -1
        Private m_LastDate As DateTime
        Private m_PortFolio As Double = -1.0
        Private m_PERatio As Double = -1.0
        Private m_PBratio As Double = -1.0
        Private m_DividendYield As Double = -1.0
        Private m_SalesGrowth As Double = -1.0
        Private m_PriceMomentum As Double = -1.0
        Private m_RelativeStrength As Double = -1.0
        Private m_Beta As Double = -1.0
        Private m_isCIR As Boolean = False
        Private m_isManaged As Boolean = False
        Private m_CreatedDate As DateTime
        Private m_ModifiedDate As DateTime
        Private m_CreatedBy As Integer = -1
        Private m_ModifiedBy As Integer = -1
        Private setvalue As Double = -1.0
#End Region

#Region "Properties"

        Public Property Beta() As Double
            Get
                Return m_Beta
            End Get
            Set(ByVal value As Double)
                m_Beta = value
            End Set
        End Property

        Public Property RelativeStrength() As Double
            Get
                Return m_RelativeStrength
            End Get
            Set(ByVal value As Double)
                m_RelativeStrength = value
            End Set
        End Property

        Public Property PriceMomentum() As Double
            Get
                Return m_PriceMomentum
            End Get
            Set(ByVal value As Double)
                m_PriceMomentum = value
            End Set
        End Property

        Public Property SalesGrowth() As Double
            Get
                Return m_SalesGrowth
            End Get
            Set(ByVal value As Double)
                m_SalesGrowth = value
            End Set
        End Property

        Public Property DividendYield() As Double
            Get
                Return m_DividendYield
            End Get
            Set(ByVal value As Double)
                m_DividendYield = value
            End Set
        End Property

        Public Property PEratio() As Double
            Get
                Return m_PERatio
            End Get
            Set(ByVal value As Double)
                m_PERatio = value
            End Set
        End Property

        Public Property PBratio() As Double
            Get
                Return m_PBratio
            End Get
            Set(ByVal value As Double)
                m_PBratio = value
            End Set
        End Property

        Public Property PortFolio() As Double
            Get
                Return m_PortFolio
            End Get
            Set(ByVal value As Double)
                m_PortFolio = value
            End Set
        End Property

        Public Property FundTypeId() As Integer
            Get
                Return m_FundTypeId
            End Get
            Set(ByVal value As Integer)
                m_FundTypeId = value
            End Set
        End Property

        Public Property ModifiedBy() As Integer
            Get
                Return m_ModifiedBy
            End Get
            Set(ByVal value As Integer)
                m_ModifiedBy = value
            End Set
        End Property

        Public Property FundName() As String
            Get
                Return m_FundName
            End Get
            Set(ByVal value As String)
                m_FundName = value
            End Set
        End Property

        Public Property FundId() As Integer
            Get
                Return m_FundId
            End Get
            Set(ByVal value As Integer)
                m_FundId = value
            End Set
        End Property

        Public Property StyleId() As Integer
            Get
                Return m_styleId
            End Get
            Set(ByVal value As Integer)
                m_styleId = value
            End Set
        End Property

        Public Property TurnoverId() As Integer
            Get
                Return m_TurnoverId
            End Get
            Set(ByVal value As Integer)
                m_TurnoverId = value
            End Set
        End Property

        Public Property CountryId() As Integer
            Get
                Return m_CountryId
            End Get
            Set(ByVal value As Integer)
                m_CountryId = value
            End Set
        End Property

        Public Property CreatedDate() As DateTime
            Get
                Return m_CreatedDate
            End Get
            Set(ByVal value As DateTime)
                m_CreatedDate = value
            End Set
        End Property

        Public Property ModifyDate() As DateTime
            Get
                Return m_ModifiedDate
            End Get
            Set(ByVal value As DateTime)
                m_ModifiedDate = value
            End Set
        End Property

        Public Property CreatedBy() As Integer
            Get
                Return m_CreatedBy
            End Get
            Set(ByVal value As Integer)
                m_CreatedBy = value
            End Set
        End Property

        Public Property ModifyBy() As Integer
            Get
                Return m_ModifiedBy
            End Get
            Set(ByVal value As Integer)
                m_ModifiedBy = value
            End Set
        End Property

        Public Property LastReportDate() As DateTime
            Get
                Return m_LastDate
            End Get
            Set(ByVal value As DateTime)
                m_LastDate = value
            End Set
        End Property

        Public Property FndEntityId() As String
            Get
                Return m_FndEntityID
            End Get
            Set(ByVal value As String)
                m_FndEntityID = value
            End Set
        End Property

        Public Property EStatusID() As String
            Get
                Return m_StatusID
            End Get
            Set(ByVal value As String)
                m_StatusID = value
            End Set
        End Property

        Public Property IsCIRFund() As Boolean
            Get
                Return m_isCIR
            End Get
            Set(ByVal value As Boolean)
                m_isCIR = value
            End Set
        End Property

        Public Property IsManaged() As Boolean
            Get
                Return m_isManaged
            End Get
            Set(ByVal value As Boolean)
                m_isManaged = value
            End Set
        End Property

#End Region

#Region "Functions"

        Public Shared Function CheckFund(ByVal fundid As String) As Boolean
            Dim baseclass As BaseClassDataContext = Nothing
            'Try
            baseclass = New BaseClassDataContext(BLLmdlCommon.strConfiguration)
            Dim query = (From p In baseclass.GetTable(Of Cp_Fund)() _
                       Where p.FundID = fundid Select p).SingleOrDefault
            If (query Is Nothing) Then
                Return False
            Else
                Return True
            End If
            'Catch ex As Exception
            '    Return False
            'End Try
        End Function


        Public Shared Function Delete_Funds(ByVal InvestorId As String) As String

            Dim dtReturn As ExecutionResult = Nothing
            Dim Logmessage As New System.Text.StringBuilder
            Dim param(0) As SqlParameter
            param(0) = New SqlParameter("@Fund_Id", InvestorId)
            'Try
            dtReturn = CoreFunctions.Execute(strConfiguration, "CR_sp_FND_DeleteFund", CommandType.StoredProcedure, ReturnTypes.Scalar, param)
            If (CType(dtReturn.ResultObject, String) <> "Fund deleted successfully") Then
                '''''Logging
                Logmessage.Append("FundID - ")
                Logmessage.Append(InvestorId)
                Logmessage.Append(Environment.NewLine)
                Logmessage.Append("Fund cannot be deleted successfully.")
                Logmessage.Append(Environment.NewLine)
                Logmessage.Append("Error Message - ")
                Logmessage.Append(CType(dtReturn.ResultObject, String))
                CreateErrorLog(Logmessage)
                ''''''''''''''''''''''''''''
            Else
                '''''Logging
                Logmessage.Append("FundID - ")
                Logmessage.Append(InvestorId)
                Logmessage.Append(Environment.NewLine)
                Logmessage.Append("Fund deleted successfully.")
                CreateErrorLog(Logmessage)
                ''''''''''''''''''''''''''''
            End If
            Logmessage = Nothing
            Return CType(dtReturn.ResultObject, String)
            'Return dtReturn
            'Catch ex As Exception
            '    Throw New Exception("Data can not be inserted.")
            'End Try
        End Function

        Public Function InsertUpdateFundDetails(ByVal pintFundId As Integer, ByVal pblnFromDataHub As Boolean, ByVal pintProjectID As Integer, ByVal pstrFndEntityID As String) As DataSet
            Dim dtReturn As DataSet = Nothing
            Dim Logmessage As New System.Text.StringBuilder
            Dim intErrorCheck As Integer = 0
            Dim eReturnVal As ExecutionResult = Nothing
            While (True)
                If Not pblnFromDataHub Then
                    Dim param(19) As SqlClient.SqlParameter
                    param(0) = New SqlParameter("@FundId", FundId)
                    param(1) = New SqlParameter("@FundName", FundName)
                    param(2) = New SqlParameter("@EStatusID", EStatusID)
                    param(3) = New SqlParameter("@FundTypeID", FundTypeId)
                    param(4) = New SqlParameter("@StyleID", StyleId)
                    param(5) = New SqlParameter("@TurnoverID", TurnoverId)
                    param(6) = New SqlParameter("@CountryID", CountryId)
                    If LastReportDate = "#12:00:00 AM#" Then
                        param(7) = New SqlParameter("@LastReportDate", SqlDateTime.Null)
                    Else
                        param(7) = New SqlParameter("@LastReportDate", LastReportDate)
                    End If
                    If PortFolio = setvalue Then
                        param(8) = New SqlParameter("@PortFolioValue", SqlDecimal.Null)
                    Else
                        param(8) = New SqlParameter("@PortFolioValue", PortFolio)
                    End If
                    If PEratio = setvalue Then
                        param(9) = New SqlParameter("@PERatio", SqlDecimal.Null)
                    Else
                        param(9) = New SqlParameter("@PERatio", PEratio)
                    End If
                    If PBratio = setvalue Then
                        param(10) = New SqlParameter("@PBRatio", SqlDecimal.Null)
                    Else
                        param(10) = New SqlParameter("@PBRatio", PBratio)
                    End If
                    If DividendYield = setvalue Then
                        param(11) = New SqlParameter("@DividendYield", SqlDecimal.Null)
                    Else
                        param(11) = New SqlParameter("@DividendYield", DividendYield)
                    End If
                    If SalesGrowth = setvalue Then
                        param(12) = New SqlParameter("@SalesGrowth", SqlDecimal.Null)
                    Else
                        param(12) = New SqlParameter("@SalesGrowth", SalesGrowth)
                    End If
                    If PriceMomentum = setvalue Then
                        param(13) = New SqlParameter("@PriceMomentum", SqlDecimal.Null)
                    Else
                        param(13) = New SqlParameter("@PriceMomentum", PriceMomentum)
                    End If
                    If RelativeStrength = setvalue Then
                        param(14) = New SqlParameter("@RelativeStrength", SqlDecimal.Null)
                    Else
                        param(14) = New SqlParameter("@RelativeStrength", RelativeStrength)
                    End If
                    If Beta = setvalue Then
                        param(15) = New SqlParameter("@Beta", SqlDecimal.Null)
                    Else
                        param(15) = New SqlParameter("@Beta", Beta)
                    End If
                    param(16) = New SqlParameter("@IsCIRFund", IsCIRFund)
                    param(17) = New SqlParameter("@IsManaged", IsManaged)
                    param(18) = New SqlParameter("@CreatedBy", CreatedBy)
                    param(19) = New SqlParameter("@ModifiedBy", ModifiedBy)
                    eReturnVal = CoreFunctions.Execute(strConfiguration, "CR_sp_FND_AddUpdateFundDetails", CommandType.StoredProcedure, ReturnTypes.DataSet, param)
                    dtReturn = CType(eReturnVal.ResultObject, DataSet)
                Else
                    Dim param(21) As SqlClient.SqlParameter
                    param(0) = New SqlParameter("@FundId", FundId)
                    param(1) = New SqlParameter("@FundName", FundName)
                    param(2) = New SqlParameter("@EStatusID", EStatusID)
                    param(3) = New SqlParameter("@FundTypeID", FundTypeId)
                    param(4) = New SqlParameter("@StyleID", StyleId)
                    param(5) = New SqlParameter("@TurnoverID", TurnoverId)
                    param(6) = New SqlParameter("@CountryID", CountryId)
                    If LastReportDate = "#12:00:00 AM#" Then
                        param(7) = New SqlParameter("@LastReportDate", SqlDateTime.Null)
                    Else
                        param(7) = New SqlParameter("@LastReportDate", LastReportDate)
                    End If
                    If PortFolio = setvalue Then
                        param(8) = New SqlParameter("@PortFolioValue", SqlDecimal.Null)
                    Else
                        param(8) = New SqlParameter("@PortFolioValue", PortFolio)
                    End If
                    If PEratio = setvalue Then
                        param(9) = New SqlParameter("@PERatio", SqlDecimal.Null)
                    Else
                        param(9) = New SqlParameter("@PERatio", PEratio)
                    End If
                    If PBratio = setvalue Then
                        param(10) = New SqlParameter("@PBRatio", SqlDecimal.Null)
                    Else
                        param(10) = New SqlParameter("@PBRatio", PBratio)
                    End If
                    If DividendYield = setvalue Then
                        param(11) = New SqlParameter("@DividendYield", SqlDecimal.Null)
                    Else
                        param(11) = New SqlParameter("@DividendYield", DividendYield)
                    End If
                    If SalesGrowth = setvalue Then
                        param(12) = New SqlParameter("@SalesGrowth", SqlDecimal.Null)
                    Else
                        param(12) = New SqlParameter("@SalesGrowth", SalesGrowth)
                    End If
                    If PriceMomentum = setvalue Then
                        param(13) = New SqlParameter("@PriceMomentum", SqlDecimal.Null)
                    Else
                        param(13) = New SqlParameter("@PriceMomentum", PriceMomentum)
                    End If
                    If RelativeStrength = setvalue Then
                        param(14) = New SqlParameter("@RelativeStrength", SqlDecimal.Null)
                    Else
                        param(14) = New SqlParameter("@RelativeStrength", RelativeStrength)
                    End If
                    If Beta = setvalue Then
                        param(15) = New SqlParameter("@Beta", SqlDecimal.Null)
                    Else
                        param(15) = New SqlParameter("@Beta", Beta)
                    End If
                    param(16) = New SqlParameter("@IsCIRFund", IsCIRFund)
                    param(17) = New SqlParameter("@IsManaged", IsManaged)
                    param(18) = New SqlParameter("@CreatedBy", CreatedBy)
                    param(19) = New SqlParameter("@ModifiedBy", ModifiedBy)
                    param(20) = New SqlParameter("@ProjectID", pintProjectID)
                    param(21) = New SqlParameter("@FndEntityID", pstrFndEntityID)
                    eReturnVal = CoreFunctions.Execute(strConfiguration, "CR_sp_DE_InsertFundLog", CommandType.StoredProcedure, ReturnTypes.DataSet, param)
                    dtReturn = CType(eReturnVal.ResultObject, DataSet)
                End If
                If (dtReturn IsNot Nothing) Then
                    If (dtReturn.Tables.Count > 0) Then
                        Return dtReturn
                    End If
                End If
            End While
        End Function

        Public Function GetTotalFunds() As Integer
            Dim obj As BaseClassDataContext = Nothing
            Dim total As Integer = 0
            'Try
            obj = New BaseClassDataContext(BLLmdlCommon.strConfiguration)
            total = (From p In obj.GetTable(Of Cp_Fund)() Select p.FundID).Count
            Return total
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function

        Public Shared Function GetRegionFromCountry(ByVal regionId As Integer) As String
            Dim obj As BaseClassDataContext = Nothing
            Dim xmap As XElement = Nothing
            Dim dt As DataSet = Nothing
            ' Try
            obj = New BaseClassDataContext(BLLmdlCommon.strConfiguration)
            xmap = New XElement("GetPrices", From p In obj.CP_Regions _
                    Join t1 In obj.Cp_Countries On t1.RegionID Equals p.RegionID _
                    Where t1.CountryID = regionId _
                    Select New XElement("Title", _
            New XElement("region", p.Region)))
            dt = New DataSet
            dt.ReadXml(New System.IO.StringReader(xmap.ToString()))
            If dt.Tables.Count > 0 Then
                If (dt.Tables(0).Rows.Count > 0) Then
                    Return dt.Tables(0).Rows(0).ItemArray.GetValue(0).ToString()
                Else
                    Return ""
                End If
            Else
                Return ""
            End If
            'Catch ex As Exception
            '    Return ""
            'End Try
        End Function

        Public Shared Function GetOverViewDataSet() As IDataReader
            Dim obj As ExecutionResult = Nothing
            obj = CoreFunctions.Execute(strConfiguration, "CR_sp_FND_GetDetails", CommandType.StoredProcedure, ReturnTypes.DataReader)
            Return CType(obj.ResultObject, IDataReader)
        End Function

        Public Function GetFundByID(ByVal Id As String) As DataSet
            Dim sp As String = "CR_sp_FND_GetFundType"
            Try
                Return CreateDataBase().ExecuteDataSet(sp, New Object() {Id})
            Catch ex As Exception
                Throw ex
                Return Nothing
            End Try
        End Function

        ''' <summary>
        ''' Loads all investor
        ''' </summary>
        ''' <param name="FunDID"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function SearchParticularFund(ByVal FundID As Integer) As IDataReader
            Dim dtReturn As IDataReader
            dtReturn = CreateDataBase().ExecuteReader("CR_sp_FND_GetFundDetail", New Object() {FundID})
            Return dtReturn
        End Function

        Public Shared Function SearchFundDetail(ByVal intSearchin As Integer, ByVal strLookFor As String, ByVal Criteria As Short) As IDataReader

            Dim eReturnVal As ExecutionResult = Nothing
            Dim param(2) As SqlParameter
            param(0) = New SqlParameter("@SearchIn", intSearchin)
            param(1) = New SqlParameter("@LookFor", strLookFor)
            param(2) = New SqlParameter("@SearchCriteria", Criteria)

            eReturnVal = CoreFunctions.Execute(strConfiguration, "CR_sp_FND_SearchFunds", CommandType.StoredProcedure, ReturnTypes.DataReader, param)
            Return CType(eReturnVal.ResultObject, IDataReader)
        End Function
        Public Shared Sub UpdateFundNLE(ByVal alertId As Integer, ByVal entityId As Integer, ByVal primaryKeyId As Integer)
            Dim objClass As New BaseClassDataContext(BLLmdlCommon.strConfiguration)
            ' Try
            Dim updatedval = (From a In objClass.cp_ALT_Alerts _
                                       Where a.AlertID = alertId And a.EntityID = entityId And a.PrimaryKeyID = primaryKeyId _
                                       Select a.UpdateColumnValue).SingleOrDefault
            Dim UpdateNLE = (From p In objClass.GetTable(Of Cp_Fund)() _
                                   Where p.FundID = primaryKeyId _
                                   Select p).SingleOrDefault
            With UpdateNLE
                .NLE = updatedval
                objClass.SubmitChanges()
            End With
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Sub
        Public Shared Function GetFundByFundID(ByVal fundID As Integer) As IDataReader
            Dim param(0) As SqlParameter
            param(0) = New SqlParameter("@FundID", fundID)
            Dim obj As ExecutionResult = Nothing
            obj = CoreFunctions.Execute(strConfiguration, "CR_sp_FND_GetFund", CommandType.StoredProcedure, ReturnTypes.DataReader, param)
            Return CType(obj.ResultObject, IDataReader)
        End Function

        Public Shared Function GetFundNameCount(ByVal FundName As String) As DataSet
            Dim sp As String = "CR_sp_FND_GetFundNameCount"
            Try
                Return CreateDataBase().ExecuteDataSet(sp, New Object() {FundName})
            Catch ex As Exception
                Throw ex
                Return Nothing
            End Try
        End Function

        Public Shared Function GetInvestorByFundID(ByVal fundid As Integer) As IDataReader
            Dim dtReturn As IDataReader = Nothing
            dtReturn = CreateDataBase().ExecuteReader("CR_sp_FND_GetInvestor", New Object() {fundid})
            Return dtReturn
        End Function

        Public Shared Function GetInvestorsByNameforFunds(ByVal InvestorName As String) As IDataReader
            Dim eReturnVal As ExecutionResult = Nothing
            Dim param(0) As SqlClient.SqlParameter
            param(0) = New SqlParameter("@InvestorName", InvestorName)
            eReturnVal = CoreFunctions.Execute(strConfiguration, "CR_sp_FND_GetInvestorsName", CommandType.StoredProcedure, ReturnTypes.DataReader, param)
            Return CType(eReturnVal.ResultObject, IDataReader)
        End Function

        Public Sub InsertFundInvestorLinking(ByVal FundID As Integer, ByVal InvestorID As String)
            Dim eReturnVal As ExecutionResult = Nothing
            Dim param(1) As SqlParameter
            param(0) = New SqlParameter("@FundID", FundID)
            param(1) = New SqlParameter("@CMBdoc", InvestorID)
            eReturnVal = CoreFunctions.Execute(strConfiguration, "CR_sp_FND_LinkInvestor", CommandType.StoredProcedure, ReturnTypes.NonQuery, param)
        End Sub

        Public Shared Function GetNewLinkID(ByVal pintProjectId As Integer, ByVal pintLinkID As Integer, ByVal pintInvestorId As Integer, ByVal pintFundID As Integer) As String
            Dim dtReturn As ExecutionResult = Nothing
            Dim Logmessage As New System.Text.StringBuilder
            Dim param(3) As SqlParameter
            param(0) = New SqlParameter("@ProjectId", pintProjectId)
            param(1) = New SqlParameter("@LinkID", pintLinkID)
            param(2) = New SqlParameter("@InvestorID", pintInvestorId)
            param(3) = New SqlParameter("@FundID", pintFundID)
            dtReturn = CoreFunctions.Execute(strConfiguration, "CR_sp_DE_ChangedFund", CommandType.StoredProcedure, ReturnTypes.Scalar, param)
            Return dtReturn.ResultObject
        End Function

#End Region
    End Class
End Namespace